import app from './app'
import { config } from './config'
import logger from './config/logger'

const PORT = process.env.PORT ? parseInt(process.env.PORT) : config.server.port

app
  .listen(PORT, '0.0.0.0', () => {
    logger.info(`Server running on port ${config.server.url}:${PORT}`)
  })
  .on('error', (err: NodeJS.ErrnoException) => {
    if (err.code === 'EADDRINUSE') {
      logger.error(
        `Port ${PORT} is already in use. Please use a different port.`
      )
      process.exit(1)
    } else {
      logger.error('Failed to start server:', err)
      process.exit(1)
    }
  })
